package leetcodeHis;

public class Algo_2_TwoPlus {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(2);
        l1.next = new ListNode(4);
        l1.next.next = new ListNode(3);

        ListNode l2 = new ListNode(5);
        l2.next = new ListNode(6);
        l2.next.next = new ListNode(4);

        ListNode result = findTwoSum(l1, l2);
        while (result!=null) {
            System.out.println(result.val);
            result = result.next;
        }
    }

    private static ListNode findTwoSum(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(-1);
        ListNode head = result;

        int add = 0;
        while (l1!=null || l2!=null || add>0) {
            int sum = 0;
            if(l1!=null) {
                sum = sum + l1.val;
                l1 = l1.next;
            }
            if(l2!=null) {
                sum = sum + l2.val;
                l2 = l2.next;
            }
            if(add>0) {
                sum = sum + (add--);
            }
            if(sum>9) {
                add++;
                sum = sum%10;
            }
            head.next = new ListNode(sum);
            head = head.next;
        }
        return result.next;
    }
}
